package com.harmontronics.hanma.paas.paas.domain.organization.repository.persistence;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.harmontronics.hanma.paas.paas.consts.NumberEnum;
import com.harmontronics.hanma.paas.paas.domain.organization.entity.SubmitterUser;
import com.harmontronics.hanma.paas.paas.domain.organization.entity.valueobject.HmOrgFields;
import com.harmontronics.hanma.paas.paas.domain.organization.repository.mapper.HmUserMapper;
import com.harmontronics.hanma.paas.paas.domain.organization.repository.po.HmOrgPO;
import com.harmontronics.hanma.paas.paas.domain.organization.repository.po.HmUserPO;
import java.util.HashMap;
import java.util.Map;
import org.springframework.stereotype.Repository;

import java.util.List;


@Repository("hmUserRepository")
public class HmUserRepository extends ServiceImpl<HmUserMapper, HmUserPO> {

    public IPage<HmUserPO> selectPage(QueryWrapper<HmUserPO> queryWrapper, Page<HmUserPO> page) {
        return baseMapper.selectPage(page, queryWrapper);
    }


    public List<SubmitterUser> selectSubmitterUserIn(List<Long> submitterIds) {
        return baseMapper.selectSubmitterUserIn(submitterIds);
    }

    public List<HmUserPO> findByManageOrg(Long orgId) {
        QueryWrapper<HmUserPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("manage_org", "[" + orgId + "]");
        return baseMapper.selectList(queryWrapper);
    }

    public Map<Long, HmUserPO> findAllUsers(){
        Map<Long, HmUserPO> userMap = new HashMap<>();
        List<HmUserPO> allUsers = list(
                new QueryWrapper<HmUserPO>()
                        .eq(HmOrgFields.DELETED, NumberEnum.ZERO.getNum()));
        if(CollectionUtil.isNotEmpty(allUsers)){
            allUsers.forEach(item->{
                userMap.put(item.getId(),item);
            });
        }
        return userMap;
    }
}